package com.yuliang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuliang.constants.SystemConstants;
import com.yuliang.domain.R;
import com.yuliang.domain.dto.UpdateCategoryDto;
import com.yuliang.domain.entity.Article;
import com.yuliang.domain.entity.Category;
import com.yuliang.domain.entity.Tag;
import com.yuliang.domain.vo.CategoryVo;
import com.yuliang.domain.vo.PageVo;
import com.yuliang.domain.vo.TagVo;
import com.yuliang.mapper.CategoryMapper;
import com.yuliang.service.ArticleService;
import com.yuliang.service.CategoryService;
import com.yuliang.utils.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 分类表(Category)表服务实现类
 *
 * @author makejava
 * @since 2023-09-19 16:19:31
 */
@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private ArticleService articleService;

    @Override
    public R getCategoryList() {
        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Article::getStatus, SystemConstants.ARTICLE_STATUS_NORMAL);//要求正常发布的文章的分类信息
        List<Article> list = articleService.list(queryWrapper);
        //文章分类id去重
        Set<Long> set = new HashSet<>();
        for (Article article : list) {
            set.add(article.getCategoryId());
        }
        //vo
        List<Category> categories = listByIds(set);//根据分类id查询分类信息
        List<CategoryVo> categoryVos = BeanCopyUtils.copyBeanList(categories, CategoryVo.class);
        return R.okResult(categoryVos);
    }

    @Override
    public R getListAllCategory() {
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Category::getStatus,SystemConstants.STATUS_NORMAL);
        List<CategoryVo> categoryVos = BeanCopyUtils.copyBeanList(list(wrapper), CategoryVo.class);
        return R.okResult(categoryVos);
    }

    @Override
    public R addCategory(CategoryVo categoryVo) {
        Category category = BeanCopyUtils.copyBean(categoryVo, Category.class);
        save(category);
        return R.okResult();
    }

    @Override
    public R getCategoryDetail(Long id) {
        Category category = getById(id);
        CategoryVo categoryVo = BeanCopyUtils.copyBean(category, CategoryVo.class);
        return R.okResult(categoryVo);
    }

    @Override
    public R updateCategory(UpdateCategoryDto updateCategoryDto) {
        Category category = BeanCopyUtils.copyBean(updateCategoryDto, Category.class);
        updateById(category);
        return R.okResult();
    }

    @Override
    public R deleteCategory(List<Long> ids) {
        removeByIds(ids);
        return R.okResult();
    }

    @Override
    public R categoryPages(Integer pageNum, Integer pageSize,String name,String status) {
        //需要分页查询分类列表 | 能根据分类名称进行模糊查询 | 能根据状态进行查询。
        IPage<Category> page = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();//状态正常，根据name or status 查询
        queryWrapper.like(StringUtils.hasText(name), Category::getName,name);
        queryWrapper.like(StringUtils.hasText(status),Category::getStatus,status);
        page(page,queryWrapper);
        List<CategoryVo> categoryVos = BeanCopyUtils.copyBeanList(page.getRecords(), CategoryVo.class);

        return R.okResult(new PageVo(categoryVos,page.getTotal()));
    }
}
